/*
 * uart_debug.c
 *
 *  Created on: 2021年8月27日
 *      Author: Administrator
 */

#include "board.h"
#include "uart_debug.h"
#include "mlog.h"
#include "settings.h"
#include <string.h>
#include "string_tools.h"
#include "soft_timer.h"
uint32_t uart_protocol_len(uint8_t* data, uint32_t len)
{
	uint32_t i = 0;
	for(i = 0; i < len; i++)
	{
		/*结束符*/
		if((data[i] == '\r') || (data[i] == '\n') || (data[i] == 0))
		{
			return i;
		}
	}
	return len;
}
/*设置系统时间*/
void update_system_datetime(void)
{
	uint16_t datetime[6]={0};
	uint8_t i=0,j=0,flag=0;
	for(i=0;i<SystemSetting[SETTINGS_SYS_DATETIME].len;i++)
	{
		if((SystemSetting[SETTINGS_SYS_DATETIME].nodePtr[i] >= '0')&&(SystemSetting[SETTINGS_SYS_DATETIME].nodePtr[i]<='9'))
		{
			flag=1;
			datetime[j]=datetime[j]*10+(SystemSetting[SETTINGS_SYS_DATETIME].nodePtr[i]-'0');
			
			continue;
		}
		if(flag==1)
		{
			j++;
			flag=0;
		}
		
	}
	if(flag==1)
	{
		j++;
		flag=0;
	}
	if(j==6)
	{
		mprintf(LOG_INFO,"%d-%d-%d %d:%d:%d.\r\n",datetime[0]%100,datetime[1],datetime[2],datetime[3],datetime[4],datetime[5]);
		//set_RTCDateTime(datetime[0]%100,datetime[1],datetime[2],datetime[3],datetime[4],datetime[5]);
	}
	else
	{
		mprintf(LOG_ERROR,"时间格式错误。\r\n");
	}
}
/*
 * biref:LOG串口数据解析
 */
uint8_t uart_protocol(uint8_t* data, uint32_t len)
{

	if((data[0] == '$') && (data[1] == 'P'))
	{
		uint32_t key = string_get_substring_number(data, "$P");
		if(NO_NUM != key)
		{
			char* p =	strstr((char*)data, "=");
			/*获取去掉结束符后的总长度*/
			len = uart_protocol_len(data, len);
			/*计算参数长度*/
			len = len - ((uint32_t)(p+1) - (uint32_t)data);
			System_SettingsSetSingle(key,  len, (uint8_t*)(p + 1));
			
			log_uart_send((uint8_t*)"$OK\r\n", 5);

			switch(key)
			{
			case SETTINGS_REBOOT:
				{
					if((p + 1)[0] == '1')
					{
						soft_reboot();
					}
					break;
				}
			case SETTINGS_SYS_DATETIME:
				{
					update_system_datetime();
					break;
				}
			default:
				{
					break;
				}
			}

		}
		else
		{
			mprintf(LOG_ERROR, "param error.\r\n");
		}
		return 1;
	}
	else if((data[0] == '$') && (data[1] == '$'))
	{
		System_SettingsReport();
	}
	else if((data[0] == '$') && (data[1] == 'R') && (data[2] == 'S') && (data[3] == 'T'))
	{
		System_SettingsRestore();
	}
	return 0;
}

SerialBuf log_uart_buf = {{0}, 0, 0, 0};

void log_uart_buf_init(void)
{
	memset(log_uart_buf.DataBuf, 0, SERIAL_MAX_BUF_SIZE);
	log_uart_buf.DataLen = 0;
	log_uart_buf.recFlag = 0;
	log_uart_buf.RecTime = 0;
}

void log_uart_receive(uint8_t data)
{
	log_uart_buf.DataBuf[log_uart_buf.DataLen++] = data;
	log_uart_buf.RecTime = getSystemTime();
}

void log_uart_receive_check(void)
{
	if(log_uart_buf.recFlag==1)
    return;
  if(log_uart_buf.DataLen==0)
    return;
  if((getSystemTime()-log_uart_buf.RecTime) > LOG_UART_REC_TIMEOUT)
    log_uart_buf.recFlag=1;
}

void log_uart_handle(void)
{
	if(log_uart_buf.recFlag)
	{
		if(log_uart_buf.DataLen > 0)
		{
			//mprintfhex("receive data form log uart:", log_uart_buf.DataBuf, log_uart_buf.DataLen);
			if(0 == uart_protocol(log_uart_buf.DataBuf, log_uart_buf.DataLen))
			{
	#if BOARD_VERSION == METER_NB_V20
				nb_serial_send_data(log_uart_buf.DataBuf, log_uart_buf.DataLen);
	#elif BOARD_VERSION == METER_485_V30
				rs485_uart_send(log_uart_buf.DataBuf, log_uart_buf.DataLen);
	#elif BOARD_VERSION == LORA_METER_FM_V1_0
				lora_serial_send_data(log_uart_buf.DataBuf, log_uart_buf.DataLen);
	#else
				#error "错误的主板类型。"
	#endif
				
				
			}
		}
		log_uart_buf_init();
	}
}




